<?php


//旧版api的部分备份

class BackUpController extends BaseController {


    //搜索列表api版
	public function searchBookApi(){
		include( app_path()."/libraries/Snoopy.class.php");
		include( app_path()."/libraries/simple_html_dom.php");

		if( !Input::has('content') || strlen(Input::get('content')) == 0 ){
			return Response::json( ['error'=>true , 'message'=>'搜索内容不能为空'] );
		}

		$user_id 	= Input::has('user_id') ? Input::get('user_id') : 0;
		$content 	= Input::get('content');
		$page 		= Input::has('page') ? Input::get('page') : 0;
		$button 	= Input::get('button');

		$this->data['content'] = rawurldecode($content);

		$agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36';//伪装浏览器
		$library_ip = "202.206.242.99";
		$library_referer = "http://202.206.242.99/opac/search.php";

		$snoopy2 = new Snoopy;
		$snoopy2->host = $library_ip;
		$snoopy2->referer = $library_referer;
		$snoopy2->agent = $agent;
		$snoopy2->cookies['PHPSESSID'] = Session::get('pre_session');

		$search_url = "http://202.206.242.99/opac/openlink.php";
		$search_url .= "?strSearchType=title";
		$search_url .= "&match_flag=forward";
		$search_url .= "&historyCount=1";
		$search_url .= "&strText=".urlencode($content);
		$search_url .= "&doctype=ALL";
		$search_url .= "&displaypg=20";
		$search_url .= "&showmode=list";
		$search_url .= "&sort=CATA_DATE";
		$search_url .= "&orderby=desc";
		$search_url .= "&location=ALL";
		$search_url .= "&page=".$page;

		$snoopy2->fetch($search_url);	//var_dump($search_url);exit;
		$result2 = $snoopy2->results;

		if( $snoopy2->timed_out || !strpos($result2, "所有图书") ){
			// $this->forgetSession();
			// return Response::json( ['error'=>true , 'message'=>'请求超时'] , 200 );
			$this->data['search_error'] = 1;
			$this->data['search_error_msg'] = "搜索失败";
		}


		if( $button == 1 ){
			//插入搜索记录
			$target_result = DB::table('user_search_history')->select('*')->where('user_id','=',$user_id)->get();
			if( $user_id > 0 ){
				if( count($target_result) >= 10 ){
					$target_delete = DB::table('user_search_history')
										->where('user_id','=',$user_id)
										->orderBy('add_time' , 'ASC')
										->skip(0)
										->take(count($target_result)-9)
										->delete();
				}
			}
			$insert_arr = array(
				'user_id' => $user_id ,
				'content' => $content
				);
			DB::table('user_search_history')->insertGetId($insert_arr);
		}


		//分析搜索内容
		$search_result_arr = array();

		$html = new simple_html_dom();
		$html->load($result2);

		$books_list = $html->find(".book_list_info");
		for( $i=0 ; $i<count($books_list) ; $i++ ){
			$temp_list_info = $books_list[$i];
			//书名
			$book_name = $temp_list_info->find("a")[0]->innertext;
			$book_name_arr = explode('.',$book_name);
			$temp_key = ($page-1)*20+$i+1;
			$book_name_arr[0] = $temp_key;
			$book_name = implode('.' , $book_name_arr);

			$book_index = $temp_list_info->find("h3")[0]->innertext;
			$temp_pos = strpos($book_index , "</a>");
			//书索引
			$book_index = substr($book_index , $temp_pos+4);
			//书籍详情链接
			$book_href = $temp_list_info->find("a")[0]->href;
			//书类型
			$book_type = $temp_list_info->find("span")[0]->innertext;

			$rent_detail = $temp_list_info->find("span")[1]->innertext;
			$rent_detail_arr = explode("<br>", $rent_detail);
			//馆藏复本
			$collect_num = $rent_detail_arr[0];
			//可借复本
			$rent_num = $rent_detail_arr[1];

			$all_detail = $temp_list_info->find("p")[0]->innertext;
			$all_detail_temp = explode("</span>" , $all_detail);
			$all_detail_temp_arr = explode("<br />" , $all_detail_temp[1]);
			//作者
			$book_author = $all_detail_temp_arr[0];
			//出版社
			$book_press = $all_detail_temp_arr[1];

			$search_result_arr[] = array(
				'name' => $book_name ,
				'index' => $book_index ,
				'href' => $book_href ,
				'author' => $book_author ,
				'press' => $book_press ,
				'type' => $book_type ,
				'collect' => trim($collect_num) ,
				'rent' => trim($rent_num)
				);
		}

		//查找分页信息
		$page_info = $html->find(".pagination")[0];
		$all_fonts = $page_info->find("font");

		$currentPage = isset($all_fonts[0]) ? $all_fonts[0]->innertext : 1;

		$allPage = isset($all_fonts[1]) ? $all_fonts[1]->innertext : 1;

		$page_arr = array(
			'current' => $currentPage ,
			'all' => $allPage
			);

		//$this->data['search_result'] = $search_result_arr;

		//return View::make('search_list' , $this->data);
		return Response::json( ['error'=>false , 'result'=>$search_result_arr , 'page'=>$page_arr , 'content'=>$content] , 200 );
	}

}

?>
